package com.wzy.authority.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wzy.authority.base.dto.SysRoleDto;
import com.wzy.authority.base.request.PageParameter;
import com.wzy.authority.base.result.Results;
import com.wzy.authority.domain.SysRole;
import com.wzy.authority.domain.SysRolePermission;
import com.wzy.authority.mapper.SysPermissionMapper;
import com.wzy.authority.mapper.SysRoleMapper;
import com.wzy.authority.mapper.SysRolePermissionMapper;
import com.wzy.authority.mapper.SysRoleUserMapper;
import com.wzy.authority.service.SysRoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @program: authorityManagement
 * @description: 角色服务实现类
 * @author: 1
 * @create: 2020-03-14 20:14
 **/
@Service
@Slf4j
public class SysRoleServiceImpl implements SysRoleService {


    @Resource
    private SysRoleMapper sysRoleMapper;

    @Resource
    private SysRoleUserMapper sysRoleUserMapper;

    @Resource
    private SysPermissionMapper sysPermissionMapper;

    @Resource
    private SysRolePermissionMapper sysRolePermissionMapper;


    @Override
    public Results<Page<SysRole>> getRoleListByPage(PageParameter pageParameter, String roleName) {
        PageHelper.startPage(pageParameter.getPageNum(), pageParameter.getPageSize());
        Page<SysRole> sysRoleList = sysRoleMapper.selectRoleList(roleName);

        return Results.success("获取角色列表成功", sysRoleList);
    }

    @Override
    public Results<Void> updateRole(SysRoleDto sysRoleDto) {
        if (judegRoleInfo(sysRoleDto.getName(),sysRoleDto.getId())){
            return Results.failure("该角色名已被占用");
        }

        //修改角色信息
        int updNum = sysRoleMapper.updateByPrimaryKeySelective(sysRoleDto);

        //修改权限信息
        //1.删除权限数据
        sysRolePermissionMapper.deleteByRoleId(sysRoleDto.getId());
        //2.获取权限列表并批量添加到数据库
        List<Integer> permissionIds = sysRoleDto.getPermissionIds();
        //移除不必要的0
        permissionIds.remove(0);
        //3.插入数据库
        if (!CollectionUtils.isEmpty(permissionIds)) {
            sysRolePermissionMapper.saveList(permissionIds,sysRoleDto.getId());
        }

        if (updNum > 0) {
            return Results.success("角色信息修改成功");
        } else {
            return Results.failure("角色信息创建失败");
        }
    }


    @Override
    public Results<Void> addRole(SysRoleDto sysRoleDto) {
        if (judegRoleInfo(sysRoleDto.getName(), null)) {
            return Results.failure("该角色名已被占用");
        }

        //插入角色数据
        int insNum = sysRoleMapper.insert(sysRoleDto);

        log.info("插入数据的id为:" + sysRoleDto.getId());

        //插入权限数据
        List<Integer> permissionIds = sysRoleDto.getPermissionIds();

        //移除0,permission id是从1开始
        permissionIds.remove(0);
        //2、保存角色对应的所有权限
        if (!CollectionUtils.isEmpty(permissionIds)) {
            sysRolePermissionMapper.saveList(permissionIds,sysRoleDto.getId());
        }

        if (insNum > 0) {
            return Results.success("角色创建成功");
        } else {
            return Results.failure("角色创建失败");
        }
    }



    @Override
    public SysRole selectSysRoleById(Integer id) {
        return sysRoleMapper.selectByPrimaryKey(id);
    }



    @Override
    public Results<List<SysRole>> getAllRoleList() {
        return Results.success("获取数据成功",sysRoleMapper.selectRoleListAll());
    }


    @Override
    public Results<Void> deleteRole(Integer id) {
        //删除角色,如果有角色被占用则不删除
        List<SysRolePermission> sysRolePermissions = sysRoleUserMapper.selectRoleUserByRoleId(id);
        if(sysRolePermissions.size() <= 0){
            //删除了角色,数据库操作自动删除权限
            sysRoleMapper.deleteByPrimaryKey(id);
            return Results.success("删除成功");
        }

        return Results.failure("还有用户在使用该角色,无法删除");
    }


    /**
     * 判断角色名称是否被占用
     *
     * @param roleName
     * @return
     */
    private Boolean judegRoleInfo(String roleName, Integer id) {
        return sysRoleMapper.selectRoleByName(roleName, id) > 0;
    }
}
